/**
 * Created by ltx on 2017/10/10.
 */

const express=require('express');
const mysql=require('mysql');

var db=mysql.createPool({host: 'localhost', user: 'root', password: '123456*', database: 'myblog'});

module.exports=function () {
    var router=express.Router();

    router.post('/message',(req,res)=>{
        //console.log(req.body);
        var article_id=req.body.article_id;
        var message=req.body.message;
        var username=req.body.username;

        //姓名是否为空
        if (message==''){
            res.json({
                code:1,
                message:'你的信息呐？'
            });
            return;
        }
        else {
            db.query(`INSERT INTO message_table (articleID, sender, description)
            VALUES('${article_id}', '${username}', '${message}')`, (err, data)=>{
                if (err){
                    res.status(500).send('数据库增加失败').end();
                }
                else {
                    res.json({
                        code:0,
                        message:'发送成功！'
                    });
                    return;
                }
            })
        }
    })
    return router;
}